home *** CD-ROM | disk | FTP | other *** search
/ NetNews Offline 2 / NetNews Offline Volume 2.iso / news / comp / lang / c++-part1 / 8744 < prev    next >
Encoding:
Text File  |  1996-08-05  |  2.3 KB  |  84 lines

  1. Path: news.us.net!usenet
  2. From: thoth256@us.net (Evelio Perez-Albuerne)
  3. Newsgroups: comp.lang.c++
  4. Subject: Re: Will it be auto-deleted?
  5. Date: Mon, 26 Feb 1996 14:20:16 GMT
  6. Organization: US Net
  7. Message-ID: <4gsff2$21k@news.us.net>
  8. References: <1996Feb20.110314.46035@yogi.urz.unibas.ch> <4gg91j$gdc@clarknet.clark.net>
  9. NNTP-Posting-Host: thoth256.laurel.us.net
  10. X-Newsreader: Forte Free Agent 1.0.82
  11.  
  12. gusty@clark.net (Harlan Messinger) wrote:
  13.  
  14. >Song Jin (song@iso.iso.unibas.ch) wrote:
  15. >: I have a question:
  16. >: 
  17. >: void myfunction(void)
  18. >: {
  19. >:  double *mypointer = new double[100];
  20. >: 
  21. >:  .....
  22. >: 
  23. >: }
  24. >: 
  25. >: The mypointer and the buffer it pointed will be auto-deleted after returned
  26. >: from myfunction, am I right?
  27. >: 
  28.  
  29. >Absolutely not. Any memory allocated with new has to be deleted 
  30. >explicitly. 
  31.  
  32. >    delete [] mypointer;
  33.  
  34. >Otherwise, once all pointers to it have gone out of scope, it'll just sit
  35. >there unreachable until the heap itself is returned to wherever it came
  36. >from (if that happens) or garbage collection returns it to the heap (if
  37. >your system has garbage collection) or until the machine is rebooted. This
  38. >is called a "memory leak". 
  39.  
  40. An alternative to manually deleteing the pointer is to use an
  41. auto_array<T> class instead of a plain function pointer. This class is
  42. very similar to the auto_ptr<T> class which I know is being included
  43. in the Standard C++ Library. I don't know if auto_array<T> is also
  44. part of the SC++L, but it is obviously needed since "delete []" not
  45. plain "delete" needs to be called.
  46.  
  47. Here is my implementation of auto_array<T>:
  48.  
  49. template <class T>
  50. class auto_array
  51. {
  52.  public:
  53.     auto_array(T* pp = 0) : p(pp) {}
  54.     auto_array(auto_array<T>& x) : p(x.p) {x.p = 0;}
  55.     ~auto_array() {delete [] p;}
  56.  
  57.     T& operator [](int i) {return p[i];}
  58.     auto_array<T>& operator =(T* pp) {delete [] p; p = pp; return *this;}
  59.     auto_array<T>& operator =(auto_array<T>& x)
  60.         {delete [] p; p = x.p; x.p = 0; return *this;}
  61.     T* get() {return p;}
  62.     T* release() {T* pp = p; p = 0; return pp;}
  63.  
  64.  private:
  65.     T* p;
  66. };
  67.  
  68. Use it like this:
  69.  
  70. void myfunction(void)
  71. {
  72. auto_array<double> mypointer(new double[100]);
  73.  
  74.   .....
  75.  
  76. }
  77.  
  78. Now the array will automatically be deleted when mypointer goes out of
  79. scope. Hope this helps!
  80.  
  81. ********************************************************
  82. Evelio Perez-Albuerne <thoth256@us.net>
  83.  
  84.